<html>
<head><meta charset="utf-8"><title>Printing all target-specific `cfg`s in source code · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Printing.20all.20target-specific.20.60cfg.60s.20in.20source.20code.html">Printing all target-specific `cfg`s in source code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205350483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Printing%20all%20target-specific%20%60cfg%60s%20in%20source%20code/near/205350483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Printing.20all.20target-specific.20.60cfg.60s.20in.20source.20code.html#205350483">(Jul 29 2020 at 12:40)</a>:</h4>
<p>Is there a way to print all the <code>cfg</code> specific to a target for a given crate? I see <code>--print=cfg</code>, but that prints the cfgs that are <em>defined</em>, not the ones that are <em>used</em>. If this doesn't exist yet, would it be easy to add?</p>
<p>For context, <a href="http://docs.rs">docs.rs</a> thinking of switching away from building all tier-one targets by default and this would let it detect exactly which targets it should build: <a href="https://internals.rust-lang.org/t/pre-rfc-build-only-one-target-by-default-on-docs-rs/12804/5?u=jyn514">https://internals.rust-lang.org/t/pre-rfc-build-only-one-target-by-default-on-docs-rs/12804/5?u=jyn514</a></p>



<a name="205361543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Printing%20all%20target-specific%20%60cfg%60s%20in%20source%20code/near/205361543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Printing.20all.20target-specific.20.60cfg.60s.20in.20source.20code.html#205361543">(Jul 29 2020 at 14:11)</a>:</h4>
<p>It seems like that would inherently need to run before expansion.  One option is to use <code>syn</code> to extract the <code>cfg</code> attributes, and then parse the <code>cfg</code> expressions (there are several crates for that).  Unfortunately since it is pre-expansion, it can be difficult to follow the module hierarchy.  I don't see anything in rustc's expansion pass that collects any information that might be useful.  Also, IIRC, once a part of the tree is pruned by a non-passing cfg, the rest of that tree is not processed, so something like <code>#[cfg(feature="foo")] mod stuff;</code> where <code>foo</code> isn't enabled means <code>mod</code> wouldn't get processed, so any target-specific <code>cfg</code> inside would be missed.  </p>
<p>I'd imagine what you want to do would be difficult to do perfectly.</p>



<a name="205362773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Printing%20all%20target-specific%20%60cfg%60s%20in%20source%20code/near/205362773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Printing.20all.20target-specific.20.60cfg.60s.20in.20source.20code.html#205362773">(Jul 29 2020 at 14:20)</a>:</h4>
<p>yes and indeed <code>mod foo;</code> in that scenario might not even exist (or could contain non-Rust code, hypothetically)</p>



<a name="205364243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Printing%20all%20target-specific%20%60cfg%60s%20in%20source%20code/near/205364243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Printing.20all.20target-specific.20.60cfg.60s.20in.20source.20code.html#205364243">(Jul 29 2020 at 14:31)</a>:</h4>
<p>well fortunately it doesn't have to be perfect, if it runs into some error <a href="http://docs.rs">docs.rs</a> could always fall back to building all the targets</p>



<a name="205364374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Printing%20all%20target-specific%20%60cfg%60s%20in%20source%20code/near/205364374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Printing.20all.20target-specific.20.60cfg.60s.20in.20source.20code.html#205364374">(Jul 29 2020 at 14:32)</a>:</h4>
<p>I like the idea of syn + cfg expressions. <a href="https://crates.io/crates/parse_cfg">https://crates.io/crates/parse_cfg</a> looks useful for that.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>